Page({

  data: {
    inputShowed: false,
    inputVal: '',
    wordList:[],
    search:{},
    selectMovies:[], // 查询结果集合
    showWordList:true
  },
  showInput() {
    this.setData({
      inputShowed: true,
    });
  },
  hideInput() {
    this.setData({
      inputVal: '',
      inputShowed: false,
    });
  },
  clearInput() {
    this.setData({
      inputVal: '',
    });
  },
  inputTyping(e) {

    this.setData({
      inputVal: e.detail.value,
      showWordList: true
    });
    // 过滤数据，保存到wordlist中
    let search = this.data.search;
    let queryString= this.data.inputVal
    let wordlist = search.filter(function(item, index, array){
      // 如果item.title包含inputVal，则符合条件，否则不符合
      return item.title.indexOf(queryString) != -1
    })
    this.setData({wordList:wordlist})

  },
  onLoad(){
    // 获取数据保存到
    let db = wx.cloud.database()
    let search = db.collection('search')
    console.log('db');
    search.get().then(res=>{
        this.setData({search:res.data[0].subjects})
      }
    )
  },
  query(options){
    console.log(options);
    this.setData({showWordList:false})
    let title= options.currentTarget.dataset.tite
    console.log(title);
    let selectMovies =[]
    let boards = wx.getStorageSync('boards')
    for(let i =0;i < boards.length ; i++){
      let movies = boards[i].movies
      for(let j = 0;j<movies.length; j++){
        let movie = movies[j]
        if(movie.title.indexOf(title)!=-1){ // 如果，id相同，则将movie保存
          selectMovies.push(movie)
        }
      }
    }
    this.setData({selectMovies:selectMovies})


  }
});
